﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DarwinIsDead.Bullets {
    /// <summary>
    /// Basic Bullet class.
    /// </summary>
    /// </remarks>
    public class SharkTooth : Bullet {
        // Sprite
        private Texture2D sprite;
        // Color
        private Color color;
        // Movement speed;
        private float speed;
        private float range;
        private float distance;
        
        public float f_angle = 0f;
        private enum mode { MODE1, MODE2, MODE3 };
        private mode Mode = mode.MODE1;
        private bool getin = true;

        private float rotation;

        public SharkTooth(World world, bool friendly, int damage)
            : base(world, 16f, 16f, friendly, damage) {
            sprite = World.Game.Content.Load<Texture2D>("Sprites\\Bullets\\sprSharkTooth");
            color = Color.White;

            rotation = 0f;

            speed = 800f;   // Moves 800 pixels per second
            range = 500f;   // But dissipates within 500 pixels
        }

        public override void Collision(Entity other, Rectangle overlap) {
            // You can use this to determine how much a penetration power a
            // bullet has, among other things.  This one stops as soon as it
            // hits something.
            if ((!Friendly && other is Player) ||
                (Friendly && other is Zombie)) {
                Active = false;
                distance = 0f;
            }
        }

        public float correct_angle(float angle)
        {
            if (angle > Math.PI)
            {
                angle -= (float)(Math.PI);
            }
            else if (angle < -Math.PI)
            {
                angle += (float)(Math.PI);
            }

            return angle;
        }

        protected override void InternalUpdate(GameTime gameTime) {
            
            if(getin == true)
            {
                f_angle = (float)(FireAngle);
                getin = false;
            }
            // Delta time
            float dt = gameTime.ElapsedGameTime.Milliseconds / 1000f;

            // Trigonometry
            float dx = dt * speed * (float)Math.Cos(f_angle);
            float dy = dt * speed * (float)Math.Sin(f_angle);
            float dp = (float)Math.Sqrt(dx*dx + dy*dy);


            if (Mode == mode.MODE1)
            {
                if (Math.Abs(f_angle - FireAngle - Math.PI / 4) < 0.1)
                {
                    Mode = mode.MODE2;   
                }
                else
                {
                    f_angle += 0.1f;
                    f_angle = correct_angle(f_angle);
                }

            }
            else if (Mode == mode.MODE2)
            {
                if (Math.Abs(f_angle - FireAngle + Math.PI / 2) < 0.1)
                {
                    Mode = mode.MODE3;
                }
                else
                {
                    f_angle -= 0.1f;
                    f_angle = correct_angle(f_angle);
                }
            }
            else
            {
                if (Math.Abs(f_angle - FireAngle) < 0.1)
                {
                    Mode = mode.MODE1;
                }
                else
                {
                    f_angle += 0.1f;
                    f_angle = correct_angle(f_angle);
                }
            }

            // Move
            Move(dx, dy);

            // Handle dissipation over distance
            distance += dp;
            if (distance > range) {
                Active = false;
                getin = true;
                distance = 0f;
            }

            rotation += (float)(6f * Math.PI * dt);
        }

        protected override void InternalDraw(SpriteBatch spriteBatch) {
            spriteBatch.Draw(sprite, Position, null, color, rotation, Vector2.Zero, 4f, SpriteEffects.None, 0f);
        }
    }
}
